<template>
  <div class="hello">
   {{msg}}
   <router-link :to="{name:'landingPage'}">landpage</router-link>
   <bdMap/>
  </div>

</template>

<script>
import {getParameter,createScript,isWeiXin,operateCookie} from '../assets/js/common'
import {initWechatShare} from '../assets/js/wechat-share'
import bdMap from './bdMap'
export default {
  name: 'index',
  data () {
    return {
      msg: '进入首页面',
      globalPath:"http://dccs.lenovo.com.cn/",
      CookieShareId:"",
      wxJs:null,
      pageIdentiy:{
        source:"",
        shareId:"",
        newRedReceiveStatus:"",
        score:"",
        attenStatus:"",
        nickName:"",
        myself:""
      },
      shareInfo:{
        title:"佛系指数已经爆表！快来看看你的是多少？",
        image:"http://cdn.yhq.lenovo.com.cn/lenovo/lucky/img/shareImg.jpg?timestamp=3002",
        description:"还有摇一摇福利抽奖等你来！",
        link:""
      }
    }
  },
  components:{
    bdMap
  },
  methods:{
    trackUrl(){
       var self = this;
      setInterval(function(){
          var browseUrl = window.location.href.split("?")[0];
          $.ajax({
            url:self.globalPath+"/v1/lenovo/weChat/updateWaitTime.do",
            dataType:"jsonp",
            type:"post",
            data:{
              browseUrl:browseUrl,
              source:self.pageIdentiy.source
            },
            success:function(data){
              //console.log(data.errormsg);
            },
            error:function(){
              //console.log("服务器异常");
            }
          });
      },10000);
    },
    saveShareAccessRecord(){
        var self = this;
          var browseUrl = window.location.href;
          $.ajax({
            url:self.globalPath+"/v1/lenovo/weChat/saveShareAccessRecord.do",
            dataType:"jsonp",
            type:"post",
            data:{
              browseUrl:browseUrl,
              shareId:self.pageIdentiy.shareId
            },
            success:function(data){
              //console.log(data.errormsg);
            },
            error:function(){
              //console.log("服务器异常");
            }
          });
    },
    shareRecord(shareType,shareId){
      var self = this;
        var localURL = window.location.href;
         $.ajax({
            url:self.globalPath+"/v1/lenovo/weChat/shareRecord.do",
            dataType:"jsonp",
            type:"post",
            data:{
              shareType:shareType,
              shareUrl:localURL,
              shareId:shareId,
            },
            success:function(data){

            },
            error:function(){
             // alert("dd");
              //console.log("服务器异常");
              //如果此处无响应8.4版本应该跳转到车主认证页面
            }
      })
    }
  },
  created(){
      if(!isWeiXin()){
        this.$router.push({path:'/landingPage'});
      }else{
        var deviceId = operateCookie.getCookie("fenli_deviceID");
        var CookieShareId = operateCookie.getCookie("fenli_shareID");
        this.CookieShareId = CookieShareId;
       var source = unescape(getParameter("source"));
       this.pageIdentiy.source = source;
       this.pageIdentiy.shareId = unescape(getParameter("shareId"));


       //判断是否存在cookie，如果不存在则去授权
          if(deviceId==null||deviceId==""||source==""||this.pageIdentiy.shareId==""){
            if(source==""){
              alert("source为空");
              window.location.href= this.globalPath+"/v1/lenovo/card/lucky2018.do";
            }else{
              if(this.pageIdentiy.shareId==""){
                alert("shareId为空");
                window.location.href= this.globalPath+"/v1/lenovo/card/lucky2018.do?source="+source;
              }else{
                 alert("shareId不为空");
                window.location.href= this.globalPath+"/v1/lenovo/card/lucky2018.do?source="+source+"&shareId="+this.pageIdentiy.shareId;
              }
            }
          }else{
            var self = this;
            $.ajax({
              url:self.globalPath+"/v1/lenovo/lucky2018/validateUser.do",
              type:"post",
              dataType:"jsonp",
              data:{
                  shareId:self.pageIdentiy.shareId
              },
              success:function(data){
                   if(data.result=="ok"){
                      //向当前数据集中添加数据
                      self.newRedReceiveStatus = data.values.newRedReceiveStatus;
                      self.score = data.values.score;
                      self.attenStatus = data.values.attenStatus;
                      self.nickName = data.values.nickName;
                      self.myself = data.values.myself;
                      alert("nickName:"+ self.nickName);
                      alert("myself:"+ self.myself);
                      //获取用户表示成功后调用浏览记录接口
                      self.trackUrl();
                      self.saveShareAccessRecord();
                    }else{
                        if(data.values=="userNull"){
                          window.location.href= self.globalPath+"/v1/lenovo/card/lucky2018.do";
                        }
                     }
                  }
            });

          }
      }
    },
    mounted(){
      var self = this;
      initWechatShare(self.shareInfo,function(){
       self.shareRecord(0,self.pageIdentiy.shareId)
      });



    //
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
